package org.breathe.dynamic.thread.pool.core.ai;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.breathe.dynamic.thread.pool.core.ai.assistant.Assistant;
import org.breathe.dynamic.thread.pool.core.event.AlarmEvent;
import org.breathe.dynamic.thread.pool.core.executor.ThreadPoolExecutorRegistry;
import org.breathe.dynamic.thread.pool.core.thread.BreatheDtpExecutor;
import org.breathe.thread.pool.common.constant.PromptConstant;
import org.breathe.thread.pool.common.entity.BreatheDtpProperties;
import org.breathe.thread.pool.common.entity.ThreadPoolExecutorProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

/**
 * @author: breathe
 * @createTime: 2025-06-27
 */
@Slf4j
@RequiredArgsConstructor
public class BreatheDtpAiHelper {
    @Autowired
    @Getter
    private Assistant assistant;
    @Autowired
    private BreatheDtpProperties breatheDtpProperties;
    public ThreadPoolExecutorProperties getThreadPoolConfigByAi(BreatheDtpExecutor breatheDtpExecutor) {
        //TODO 真正实现
        System.out.println(JSON.toJSONString(breatheDtpExecutor));
        String jsonString = assistant.chatByBlocking(String.format(PromptConstant.REFRESH_THREAD_POOL_CONFIG, breatheDtpExecutor));
        return JSONObject.parseObject(jsonString, ThreadPoolExecutorProperties.class);
    }

// 通过实现ApplicationListener接口，实现onApplicationEvent方法，就可以监听到事件
//    @Override
//    public void onApplicationEvent(AlarmEvent event) {
//        BreatheDtpExecutor breatheDtpExecutor = event.getBreatheDtpExecutor();
//        ThreadPoolExecutorProperties threadPoolExecutorProperties = event.getThreadPoolExecutorProperties();
//        System.out.println("线程池报警事件" + JSON.toJSONString(breatheDtpExecutor));
//        ThreadPoolExecutorProperties threadPoolConfigByAi = getThreadPoolConfigByAi(breatheDtpExecutor);
//        if (threadPoolConfigByAi == null) {
//            log.warn("AI 返回线程池配置为空，跳过刷新");
//            return ;
//        }
//        breatheDtpProperties.getExecutors().stream()
//                .filter(each -> each.getThreadPoolName().equals(threadPoolExecutorProperties.getThreadPoolName()))
//                .forEach(each -> {
//                    BeanUtil.copyProperties(threadPoolConfigByAi, each);
//                });
//        ThreadPoolExecutorRegistry.refresh(breatheDtpProperties);
//    }

    // 通过@TransactionalEventListener
    @EventListener(value = AlarmEvent.class)
    @Async("breatheDynamicThreadPool")
    public void onApplicationEvent(AlarmEvent event) {
        BreatheDtpExecutor breatheDtpExecutor = event.getBreatheDtpExecutor();
        ThreadPoolExecutorProperties threadPoolExecutorProperties = event.getThreadPoolExecutorProperties();
        System.out.println("线程池报警事件" + JSON.toJSONString(breatheDtpExecutor));
        ThreadPoolExecutorProperties threadPoolConfigByAi = getThreadPoolConfigByAi(breatheDtpExecutor);
        if (threadPoolConfigByAi == null) {
            log.warn("AI 返回线程池配置为空，跳过刷新");
            return ;
        }
        breatheDtpProperties.getExecutors().stream()
                .filter(each -> each.getThreadPoolName().equals(threadPoolExecutorProperties.getThreadPoolName()))
                .forEach(each -> {
                    BeanUtil.copyProperties(threadPoolConfigByAi, each);
                });
        ThreadPoolExecutorRegistry.refresh(breatheDtpProperties);
    }
}